package com.ware.common;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.tx.TxByRegex;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.ware.controller.admin.ComConnectionController;
import com.ware.controller.admin.ComIntroduceController;
import com.ware.controller.admin.IndexController;
import com.ware.dao.ComConnection;
import com.ware.dao.ComIntroduce;
import com.ware.dao.User;
import com.ware.util.UpdateDB;


public class AllConfig extends JFinalConfig{
	

	@Override
	public void configConstant(Constants me) {
		me.setDevMode(true);
		me.setBaseViewPath("/WEB-INF/view");
		me.setViewType(ViewType.FREE_MARKER);
		me.setFreeMarkerViewExtension(".ftl");
	}

	@Override
	public void configHandler(Handlers arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new AuthInterceptor());
		me.add(new TxByRegex("(.*add*.*)|(.*delete*.*)|(.*edit*.*)"));
	}

	@Override
	public void configPlugin(Plugins me) {
		this.loadPropertyFile("jdbc.properties");
		
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);
		c3p0Plugin.start();
		
		UpdateDB.update(c3p0Plugin);
		
		ActiveRecordPlugin arp = new ActiveRecordPlugin("zero",c3p0Plugin);
		arp.setShowSql(true);
		arp.addMapping("users", User.class);
		arp.addMapping("com_connection", ComConnection.class);
		arp.addMapping("com_introduce", ComIntroduce.class);
		
		
		arp.start();
		me.add(arp);
	}

	@Override
	public void configRoute(Routes me) {
		
		me.add("/", com.ware.controller.home.IndexController.class, "/website");
		
		me.add("/admin", IndexController.class, "/admin");
		
		me.add("admin/com", ComConnectionController.class, "/admin/com");
		me.add("admin/com/intro", ComIntroduceController.class, "/admin/com");
		
	}

	public static void main(String[] args) throws Exception{
		JFinal.start("WebRoot", 8081, "/", 5);
	}
}
